﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;

using DataAccess;
using Entities;
using Common;

namespace BusinessLogic
{
    static class LoaiHang
    {
        public static Entities.LoaiHang[] SelectLoaiHang(Entities.LoaiHang nv)
        {
            Entities.LoaiHang[] list = null;
            try
            {
                //lay mo ket noi voi' chuoi~ ket noi lay' tu` file app.confg trong GUI
                string sql = Common.Constants.LoaiHang.SelectLoaiHang;
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(sql, cn);
                cmd.Parameters.Add(Common.Constants.LoaiHang.HanhDong, SqlDbType.NVarChar).Value = nv.HanhDong;
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                ArrayList arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.LoaiHang row = new Entities.LoaiHang();
                    row.LoaiHangID = int.Parse(dr[Common.Constants.LoaiHang.LoaiHangID].ToString());
                    row.MaLoaiHang = dr[Common.Constants.LoaiHang.MaLoaiHang].ToString();
                    row.TenLoaiHang = dr[Common.Constants.LoaiHang.TenLoaiHang].ToString();
                    row.GhiChu = dr[Common.Constants.LoaiHang.GhiChu].ToString();
                    row.Deleted = Boolean.Parse(dr[Common.Constants.LoaiHang.Deleted].ToString());
                    arr.Add(row);
                }
                int n = arr.Count;
                if (n == 0) { list = null; }
                list = new Entities.LoaiHang[n];
                for (int i = 0; i < n; i++)
                {
                    list[i] = (Entities.LoaiHang)arr[i];
                }
                //xoa bo nho dong ket noi
                cmd.Connection.Dispose();
                cn.Close();
                DataAccess.Connection.CloseConnection();
            }
            catch (Exception ex)
            {
                string s = ex.Message.ToString();
                list = null;
            }
            return list;
        }

        public static int InsertUpdate(Entities.LoaiHang nv)
        {
            int rt = 0;
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.LoaiHang.InsertUpdateLoaiHang, cn);
                cmd.Parameters.Add(Common.Constants.LoaiHang.HanhDong, SqlDbType.NVarChar, 50).Value = nv.HanhDong;
                cmd.Parameters.Add(Common.Constants.LoaiHang.LoaiHangID, SqlDbType.Int).Value = nv.LoaiHangID;
                cmd.Parameters.Add(Common.Constants.LoaiHang.MaLoaiHang, SqlDbType.VarChar, 50).Value = nv.MaLoaiHang;
                cmd.Parameters.Add(Common.Constants.LoaiHang.TenLoaiHang, SqlDbType.NVarChar, 200).Value = nv.TenLoaiHang;
                cmd.Parameters.Add(Common.Constants.LoaiHang.GhiChu, SqlDbType.NVarChar, 200).Value = nv.GhiChu;
                cmd.Parameters.Add(Common.Constants.LoaiHang.Deleted, SqlDbType.Bit).Value = nv.Deleted;
                rt = cmd.ExecuteNonQuery();
                //xoa bo nho dong ket noi
                cmd.Connection.Dispose();
                cn.Close();
                DataAccess.Connection.CloseConnection();
            }
            catch (Exception ex)
            {
                string s = ex.Message.ToString();
                rt = 0;
            }
            return rt;
        }

        public static int Delete(Entities.LoaiHang nv)
        {
            int rt = 0;
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.LoaiHang.DeleteLoaiHang, cn);
                cmd.Parameters.Add(Common.Constants.LoaiHang.HanhDong, SqlDbType.VarChar).Value = nv.HanhDong;
                cmd.Parameters.Add(Common.Constants.LoaiHang.MaLoaiHang, SqlDbType.VarChar).Value = nv.MaLoaiHang;
                rt = cmd.ExecuteNonQuery();
                //xoa bo nho dong ket noi
                cmd.Connection.Dispose();
                cn.Close();
                DataAccess.Connection.CloseConnection();
            }
            catch (Exception ex)
            {
                string s = ex.Message.ToString();
                rt = 0;
            }
            return rt;
        }
    }
}
